package com.ecoolex.hotel.saas.platform.graphql;

import static com.ecoolex.hotel.saas.Constant.STORE_BRAND_PARAM_KEY;
import static com.ecoolex.hotel.saas.Constant.STORE_PARAM_KEY;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.modularize.api.service.region.Region;
import com.ecoolex.framework.modularize.api.service.region.RegionService;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.Store;
import com.ecoolex.hotel.saas.entity.StoreBrand;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.platform.util.StoreLicenseUtil;
import com.ecoolex.hotel.saas.pojo.QueryStoreDTO;
import com.ecoolex.hotel.saas.pojo.StoreLicense;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "商户")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = StoreGraphql.AUTH,
	name = "商户管理",
	remark = "商户管理")
public class StoreGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-STORE";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_BELONGL = AUTH + "-BELONG";
	
	@Autowired
	private StorageService storageService;

	@Autowired
	private RegionService regionService;
	
	@Reference
	private StoreService service;
	
	@TypeField(type = Store.class, field = "brand", description = { "品牌" })
	public StoreBrand brand(@Source Store store) {
		if (Check.notNullAndGtZero(store.getBrandId())) {
			return service.getStoreBrand(store.getBrandId());
		}
		return null;
	}

	@TypeField(type = Store.class, field = "logo", description = { "logo" })
	public StorageInfo logo(@Source Store store) {
		return storageService.get(store.getLogo());
	}
	
	@TypeField(type = Store.class, field = "businessLicense", description = { "营业执照" })
	public StorageInfo businessLicense(@Source Store store) {
		return storageService.get(store.getBusinessLicense());
	}
	
	@TypeField(type = Store.class, field = "organizationCodeImg", description = { "组织机构代码证" })
	public StorageInfo organizationCodeImg(@Source Store store) {
		return storageService.get(store.getOrganizationCodeImg());
	}
	
	@TypeField(type = Store.class, field = "taxRegistration", description = { "税务登记证" })
	public StorageInfo taxRegistration(@Source Store store) {
		return storageService.get(store.getTaxRegistration());
	}
	
	@TypeField(type = Store.class, field = "identityFrontImage", description = { "法人身份证正面" })
	public StorageInfo identityFrontImage(@Source Store store) {
		return storageService.get(store.getIdentityFrontImage());
	}
	
	@TypeField(type = Store.class, field = "identityBackImage", description = { "法人身份证反面" })
	public StorageInfo identityBackImage(@Source Store store) {
		return storageService.get(store.getIdentityBackImage());
	}
	
	@TypeField(type = Store.class,
		field = "region",
		description = { "区域",
			"注册地址" })
	public Region region(@Source Store store) {
		return regionService.get(store.getRegisterCityId());
	}
	
	@QueryField(field = "belongStore", description = { "获取所属商户详情" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_BELONGL, name = "获取所属商户详情")
	public Store belongStoreBrand(PlatformMemberAuthority user) {
		user.assertAuth(AUTH_BELONGL);
		List<String> storeIdList = user.allRoleParamByKey(STORE_PARAM_KEY);
		if (!storeIdList.isEmpty()) {
			Assert.isTrue(storeIdList.size() == 1, ResultCode.VERIFY.build(2), "所属商户存在多个");
			return service.getStore(Integer.valueOf(storeIdList.get(0)));
		}
		return null;
	}


	@QueryField(field = "storeList", description = { "商户列表" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商户列表")
	public Pagination<Store> queryStore(@NonNull QueryStoreDTO query, @NonNull Page page, PlatformMemberAuthority user) {
		StoreLicense license = StoreLicenseUtil.generate(user, AUTH_QUERY);
		if (license.isEmpty()) {
			user.notAuthority();
		}
		return service.queryStore(query, license, page);
	}


	@QueryField(field = "store", description = { "商户详情" })
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商户详情")
	public Store store(@NonNull Integer storeId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		// 商户权限
		if (Check.notNull(user.roleByParam(STORE_PARAM_KEY, storeId))) {
			return service.getStore(storeId);
		}
		// 检查是否是品牌的商户
		Store store = service.getStore(storeId);
		List<String> brandIdList = user.allRoleParamByKey(STORE_BRAND_PARAM_KEY);
		for (String brandId : brandIdList) {
			if (Check.eq(store.getBrandId(), Integer.valueOf(brandId))) {
				return store;
			}
		}
		return user.notAuthority();
	}
}
